package com.jun.util;

import com.jun.pojo.entity.Food;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 校验排序字段的工具类
 */
public class FoodUtil {

  /**
   * 校验排序字段是否合法
   * @param orderField 排序字段
   */
  public static boolean legalOrderField(String orderField) {
    if (StringUtils.hasLength(orderField)) {
      Class<Food> foodClass = Food.class;
      Field[] fields = foodClass.getDeclaredFields();
      List<String> fieldList = Arrays.stream(fields).map(Field::getName).collect(Collectors.toList());
      return fieldList.contains(orderField);
    }
    return false;
  }
}
